﻿using System;
using System.Collections.Generic;

using System.Text;
using Xeno.Core;
using OpenTK;
using OpenTK.Input;

using OpenTK.Graphics.OpenGL;

namespace Xeno.GUI
{
    public class XuiTextField : XuiWidget
    {
        public static XuiTextField Active;
        [XenoProp("Hint",typeof(string))]
        public string Hint { get; set; }
        [XenoProp("Text", typeof(string))]
        public string Text
        {
            get
            {
                return mText;
            }
            set
            {
                if (mText != value)
                {
                    Cache = XenoText.CreateText(mText);
                }
                mText = value;
                
              
            }
        }
        private string mText;
        [XenoProp("TickRate",typeof(int))]
        public int TickRate { get; set; }
            bool TickOn = true;
        int NextTick = 0;
        public XenoText.XenoTextCache Cache { get; set; }
        public int ActiveIndex { get; set; }
        public XuiTextField(Vector2 size,string hint) :base(size)
        {
            Text = "";
            Hint = hint;
            TickRate = 250;
            Text = string.Empty;
            ActiveIndex = 0;
        }
        public int NextStroke = 0;
        public void KeyDown(Key key)
        {
            AddKey(key);
            NextStroke = Environment.TickCount + 1000;
        }
        public void KeyUp(Key key)
        {
        }
        public void KeyPress(Key key)
        {
            if (Environment.TickCount > NextStroke)
            {
                AddKey(key);
                NextStroke = Environment.TickCount + 250;
            }
        }
        public void AddKey(Key key)
        {
            switch (key)
            {
                case OpenTK.Input.Key.BackSpace:
                    if (Text.Length <= 0) return;
                    Text = Text.Substring(0, Text.Length - 1);
                    ActiveIndex--;
                    return;
                    break;
            }
            if (Text.Length == 0)
            {
                Text = key.ToString();
                ActiveIndex = 1;
            }
            else
            {
                Text += key.ToString();
                ActiveIndex++;
            }
        }
        public int _KeyDown = -1;
        public bool[] KeyIn = new bool[255];
        public override void DoUpdate()
        {
            bool[] nk = new bool[255];
            for (int i = 1; i < 254; i++)
            {
                
                if (XenoInput.IsKeyDown((OpenTK.Input.Key)i))
                {
                    nk[i] = true;
                }
                else
                {
                    nk[i] = false;
                }

                if (nk[i] == true)
                {
                    if (KeyIn[i] == false)
                    {
                        KeyDown((OpenTK.Input.Key)i);
                    }
                    else
                    {
                        KeyPress((OpenTK.Input.Key)i);
                    }

                }
                else
                {
                    if (KeyIn[i] == true)
                    {
                        KeyUp((OpenTK.Input.Key)i);
                    }

                }
            }

            KeyIn = nk;

            float mx = XenoInput.MouseX();
            float my = XenoInput.MouseY();
            bool but = XenoInput.IsMouseDown(OpenTK.Input.MouseButton.Left);
            Vector3 wp = Attached.WorldPosition;
            if (mx >= wp.X && mx <= wp.X + Size.X && my >= wp.Y && my <= wp.Y + Size.Y)
            {
                if (but)
                {
                    Active = this;
                }
            }

            if (Active == this)
            {

                if (Environment.TickCount > NextTick)
                {
                    if (TickOn)
                    { TickOn = false; }
                    else
                    {
                        TickOn = true;
                    }
                    NextTick = Environment.TickCount + TickRate;
                }

            }
        }
    

        public override void DoRender()
        {


            Vector3 wp = Attached.WorldPosition;

            XuiWidget.Begin2D();

            GL.Color4(System.Drawing.Color.Beige);

            GL.Begin(BeginMode.Quads);

            GL.Vertex2(wp.X, wp.Y);
            GL.Vertex2(wp.X + Size.X, wp.Y);
            GL.Color4(System.Drawing.Color.White);
            GL.Vertex2(wp.X + Size.X, wp.Y + Size.Y);
            GL.Vertex2(wp.X, wp.Y + Size.Y);
            
            GL.End();

            float CharLength = 0;
            if (Text.Length > 0)
            {
                CharLength = Cache.Width;
            }
            else
            {

            }
            float dx = CharLength;
            GL.Color4(System.Drawing.Color.Black);
            if(TickOn){
            GL.Begin(BeginMode.Lines);

            GL.Vertex2(wp.X + dx, wp.Y);
            GL.Vertex2(wp.X + dx, wp.Y + Size.Y);

            GL.End();

            }
            XuiWidget.End2D();
            XenoText.DrawText(Cache, (int)wp.X + 10, (int)wp.Y,System.Drawing.Color.DarkRed);
        }
    }
}
